package com.dev.security.manager.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specifications;
import org.springframework.stereotype.Service;

import com.dev.security.manager.beans.Account;
import com.dev.security.manager.beans.AccountRoleUnit;
import com.dev.security.manager.beans.AccountRoleUnitId;
import com.dev.security.manager.dao.AccountRepository;
import com.dev.security.manager.dao.AccountRoleUnitRepository;
import com.dev.security.manager.service.IAccountService;
import com.dev.security.util.Criteria;

/**
 * @author 潜行的青衣
 * @see
 */
@Service
public class AccountService implements IAccountService {

	@Autowired
	private AccountRepository accountRepository;
	
	@Autowired
	private AccountRoleUnitRepository accountRoleUnitRepository;
	
	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAccountService#findPager(com.dev.security.manager.beans.Account, int, int)
	 */
	@Override
	public Page<Account> findPager(final Account account, int page, int size, Sort sort) {
		Criteria<Account> criteria = new Criteria<Account>();
		if(StringUtils.isNotEmpty(account.getName())) {
			criteria.and().like("name", new StringBuilder()
				.append("%").append(account.getName()).append("%").toString());
		}
		if(StringUtils.isNotEmpty(account.getAccount())) {
			criteria.and().like("account", new StringBuilder()
				.append("%").append(account.getAccount()).append("%").toString());
		}
		Specifications<Account> spec = criteria.toSpecifications();
		Pageable pageable = new PageRequest(page - 1, size, sort);
		if(spec != null) {
			return this.accountRepository.findAll(spec, pageable);
		} else {
			return this.accountRepository.findAll(pageable);
		}
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAccountService#remove(java.lang.String[])
	 */
	@Override
	public void remove(String[] account_ids) {
		if(account_ids != null) {
			for(String account_id:account_ids) {
				this.accountRoleUnitRepository.deleteByAccountId(account_id);
				this.accountRepository.delete(account_id);
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAccountService#create(com.dev.security.manager.beans.Account)
	 */
	@Override
	public boolean create(Account account) {
		if(this.checkAccount(account.getAccount())) {
			account.setId(null);
			this.accountRepository.save(account);
			return true;
		}
		return false;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAccountService#update(com.dev.security.manager.beans.Account)
	 */
	@Override
	public boolean update(Account account) {
		if(StringUtils.equals("1", account.getStatus())) {
			//无法启用，有在用的账户
			if(this.accountRepository.count(
					account.getId(), account.getAccount(), "1") > 0) {
				return false;
			}
		}
		this.accountRepository.save(account);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAccountService#checkAccount(java.lang.String)
	 */
	@Override
	public boolean checkAccount(String account) {
		return this.accountRepository.count(account, "1") < 1;
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAccountService#findAccount(java.lang.String)
	 */
	@Override
	public Account findAccount(String id) {
		return this.accountRepository.findOne(id);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAccountService#findRoleIds(java.lang.String)
	 */
	@Override
	public List<String> findRoleIds(String account_id) {
		return this.accountRoleUnitRepository.findRoleIds(account_id);
	}

	/* (non-Javadoc)
	 * @see com.dev.security.manager.service.IAccountService#allocRoles(java.lang.String, java.lang.String[])
	 */
	@Override
	public void allocRoles(String account_id, String[] role_ids) {
		this.accountRoleUnitRepository.deleteByAccountId(account_id);
		List<AccountRoleUnit> entities = new ArrayList<AccountRoleUnit>();
		if(role_ids != null) {
			for(String role_id : role_ids) {
				entities.add(new AccountRoleUnit(new AccountRoleUnitId(account_id, role_id)));
			}
			this.accountRoleUnitRepository.save(entities);
		}
	}

}
